<?php
/**
 * 卡包管理
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2019/5/7
 * @copyright PanxSoft Inc.
 */

namespace App\Http\Controllers\Wechat\V3_1;

use App\Http\Controllers\ApiController;
use App\Models\Coupon\CouponCard;
use App\Models\Coupon\CouponTicket;
use App\Models\OpenWechat\WechatUser;
use App\Services\Utils\TimeHelper;
use Illuminate\Http\Request;


class CouponCardController extends ApiController
{
    const ROUTES = [
        'index'  => 'Wechat.v3_1.coupon-card.index',
        'store'  => 'Wechat.v3_1.coupon-card.store',
        'update' => 'Wechat.v3_1.coupon-card.update',
    ];

    /**
     * @test
     * @param Request $request
     *
     * @return array|\Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $wechat_user = WechatUser::findByOpenId($request->get('openid'));
            $this->checkItemExist($wechat_user, 'wechat_user');

        $coupon_cards = (new CouponCard)
            ->where(['wechat_user_id' => $wechat_user->getKey()])
            ->with('couponTicket')
            ->oldest('is_used')
            ->latest('created_at')
            ->get();

        return $this->response(compact('coupon_cards'));
    }

    /**
     * 领取优惠券
     *
     * @param Request $request
     *
     * @return array|\Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $wechat_user = WechatUser::findByOpenId($request->get('openid'));
            $this->checkItemExist($wechat_user, 'wechat_user');

        /** @var CouponTicket $coupon_ticket */
        $coupon_ticket = (new CouponTicket)
            ->where('code', $request->get('code'))
            ->first();
            $this->checkItemExist($coupon_ticket, 'coupon ticket code');

         // check ticket belongs to wechat user's customer
//        $customer_ids = $wechat_user->stores->pluck('customer_id');
//            $this->checkCanOperate($customer_ids->contains($coupon_ticket->customer_id));

        $coupon_card = (new CouponCard)->create([
            'code'             => TimeHelper::microTimeSerial(),
            'coupon_ticket_id' => $coupon_ticket->getKey(),
            'wechat_user_id'   => $wechat_user->getKey(),
            'started_at'       => now(),
            'ended_at'         => now()->addDay($coupon_ticket->expired_in),
            'is_used'          => CouponCard::NOT_USED,
        ]);

        return $this->response(compact('coupon_card'));
    }

    /**
     * @param Request $request
     * @param $id
     *
     * @return array|\Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $wechat_user = WechatUser::findByOpenId($request->get('openid'));
            $this->checkItemExist($wechat_user, 'wechat_user');

        /** @var CouponCard $coupon_card */
        $coupon_card = (new CouponCard)->find($id);
            $this->checkItemExist($coupon_card, 'card not exist');
            $this->checkCanOperate($coupon_card->wechat_user_id === $wechat_user->getKey(), 'card not match wechat user');
            $this->checkCanOperate($coupon_card->isDateAvailable(), 'card date not in duration');
            $this->checkUnless($coupon_card->is_used, 'already used');

        $updated = $coupon_card->updateUse();

        return $this->response(compact('updated'));
    }
}
